﻿Imports System.Data.OleDb
Imports DTO

Public Class TheLoaiDAO
    Public Shared Sub Insert(ByVal tl As TheLoaiDTO)
        Dim sql As String = "insert into THELOAI(TenTheLoai) values('" & tl.TenTheLoai & "')"
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Sub Update(ByVal tl As TheLoaiDTO)
        Dim sql As String = "Update  THELOAI set TenTheLoai ='" & tl.TenTheLoai & "'where MaTheLoai=" & tl.MaTheLoai & ""
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Function Delete(ByVal tl As TheLoaiDTO) As Boolean
        Try
            Dim sql As String = "delete from THELOAI where TenTheLoai= '" & tl.TenTheLoai & "'"
            DatabassProvider.ExcuNonQuery(sql)
            Return True
        Catch
            Return False
        End Try
    End Function

    Public Shared Function GetTheLoaiByName(ByVal Name As String) As TheLoaiDTO
        Dim sql As String = "select * from THELOAI where TenTheLoai='" & Name & "'"
        Dim dt As DataTable = DatabassProvider.ExcuQuery(sql)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim tl As New TheLoaiDTO()
            tl.MaTheLoai = CInt(Fix(dt.Rows(0).ItemArray(0)))
            tl.TenTheLoai = dt.Rows(0).ItemArray(1).ToString()
            Return tl
        End If
    End Function

    Public Shared Function GetTheLoaiByMa(ByVal Ma As Integer) As TheLoaiDTO
        Dim sql As String = "select * from THELOAI where MaTheLoai=" & Ma & ""
        Dim dt As DataTable = DatabassProvider.ExcuQuery(sql)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim tl As New TheLoaiDTO()
            tl.MaTheLoai = CInt(Fix(dt.Rows(0).ItemArray(0)))
            tl.TenTheLoai = dt.Rows(0).ItemArray(1).ToString()
            Return tl
        End If

    End Function

    Public Shared Function GetTheLoaiAll() As DataTable
        Dim sql As String = "select * from THELOAI"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

End Class
